package cn.dnui_lf_705.dnui_16110100705.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import cn.dnui_lf_705.dnui_16110100705.R;
import cn.dnui_lf_705.dnui_16110100705.bean.ResultBean;
import cn.dnui_lf_705.dnui_16110100705.listener.OnRetrofitListener;
import cn.dnui_lf_705.dnui_16110100705.model.UserModel;


public class AlterUserInfoActivity extends BaseActivity implements OnRetrofitListener<ResultBean> {
    private ImageView alter_return,user_image;
    private String alter_username,alter_password,alter_telephone,alter_address,alter_comment;
    private EditText et_alter_username,et_alter_password,et_alter_telephone;
    private EditText et_alter_address;
    private EditText et_alter_comment;
    private Button alter_commit;
    SharedPreferences sharedPreferences;
    String userid;
    private UserModel userModel;
    private LinearLayout ll_image;
    String srcPath="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_user_info);
        alter_username = getIntent().getStringExtra("username");
        alter_password = getIntent().getStringExtra("userpass");
        alter_telephone = getIntent().getStringExtra("phone");
        alter_address = getIntent().getStringExtra("address");
        alter_comment = getIntent().getStringExtra("comment");
        sharedPreferences = getSharedPreferences("yht",Context.MODE_PRIVATE);
        userid = sharedPreferences.getString("user_id","");
        initViews();
        initData();
        initEvents();
        if (setEnableSliding()) {
            ArActSlidLayout rootView = new ArActSlidLayout(this);
            rootView.bindActivity(this);//绑定需要窗口的布局：
        }
    }
    protected boolean setEnableSliding() {//默认返回是需要的，只需继承的时候重写次代码 true : 需要；false :不需要；
        return true;
    }



    @Override
    void initViews() {
            ///////////
        et_alter_username = findViewById(R.id.username1);
        et_alter_password =findViewById(R.id.userpass1);
        et_alter_telephone = findViewById(R.id.phone1);
        et_alter_address = findViewById(R.id.address1);
        et_alter_comment =findViewById(R.id.comment1);
        alter_commit = findViewById(R.id.signin1);
        et_alter_username.setText(alter_username);
        et_alter_password.setText(alter_password);
        et_alter_telephone.setText(alter_telephone);
        et_alter_address.setText(alter_address);
        et_alter_comment.setText(alter_comment);
    }

    @Override
    void initEvents() {
            alter_commit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   // commit();
                   push();
                }
            });
//            alter_return.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    finish();
//                }
//            });
//            ll_image.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Intent intent = new Intent(AlterUserInfoActivity.this,PhotoActivity.class);
//                    startActivity(intent);
//                }
//            });
    }
    void push(){
        UserModel userModel = new UserModel();
        userModel.updateUserById(userid,et_alter_username.getText().toString(),et_alter_password.getText().toString(),et_alter_telephone.getText().toString(),et_alter_address.getText().toString(),this);
    }

//    private void commit() {
//        SharedPreferences sp = getSPFile();
//        SharedPreferences.Editor editor = sp.edit();
//        editor.putString("srcPath",srcPath);
//        editor.commit();
//        userModel = new UserModel();
//        userModel.updateUserById(getUser_id(),et_alter_username.getText().toString(),et_alter_password.getText().toString(),et_alter_telephone.getText().toString(),et_alter_address.getText().toString(),this);
//
//    }

    @Override
    void initData() {   ///在这里需要上一个activity传参数
        Intent intent = getIntent();
        et_alter_username.setText(alter_username);
        et_alter_password.setText("");
        et_alter_telephone.setText(alter_telephone);
        et_alter_address.setText(alter_address);
        //SharedPreferences sp =getSPFile();
      //  srcPath = sp.getString("srcPath","");
        if(!"".equals(srcPath))
        {
            setFileTolmage();
        }
    }

    @Override
    public void onsuccess(ResultBean resultBean, int flag) {
        if(resultBean.getSuccess().equals("1")){
            showToast("修改成功");
            finish();
        }
        else{
            showToast("修改失败");
        }
    }

    @Override
    public void onFailure(String string) {
        showToast("网络错误");
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){//回调函数
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==1)
        {
            Bundle bundle = data.getExtras();
            srcPath=bundle.getString("srcPath");
            if (!srcPath.equals("")){
                setFileTolmage();
            }
        }
    }

    private void setFileTolmage() {
        try{
            FileInputStream f = new FileInputStream(srcPath);
            Bitmap bm = null;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            BufferedInputStream bis = new BufferedInputStream(f);
            bm = BitmapFactory.decodeStream(bis,null,options);
            user_image.setImageBitmap(bm);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }








}
